package com.binvshe.binvshe.organization;

import android.os.Bundle;

import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.organization.entity.OrgItemEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/9/9
 */
public class OrgListFragment extends SwipeListFragment {

    private int mPage = 0;

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new OrgListAdapter(getContext());
    }

    @Override
    public void onResume() {
        super.onResume();
        onStartLoading();
    }

    public static OrgListFragment newInstance() {
        Bundle args = new Bundle();
        OrgListFragment fragment = new OrgListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    ListDataRequestListener<OrgItemEntity> mRequestListener = new ListDataRequestListener<OrgItemEntity>(this, OrgItemEntity.class);

    @Override
    public void onStartLoading() {
        getOrgList();
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        mPage = 0;
        getData().clear();
        onStartLoading();
    }

    /**
     * 获取社团列表
     */
    private void getOrgList() {
        if (mPage == 0 || mRequestListener.getResponseDataParser().hasMore()) {
            mPage++;
            IHttpRequest request = new MyHttpRequest(getContext());
            UrlParams params = new UrlParams();
            params.put("page", mPage + "");
            params.put("size", 8 + "");
            request.get(UrlCenter.GET_ORG_LIST, params, mRequestListener);
        }
    }

}
